package com.example.vote.mapper;

import com.example.vote.entity.TpVote;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 * 投票系统-题目表-2 Mapper 接口
 * </p>
 *
 * @author 廖老师
 * @since 2023-12-04
 */
public interface TpVoteMapper extends BaseMapper<TpVote> {

    @Select("<script>" +
            "select * from tp_vote" +
            " <where> " +
            "   <if test='vname!=null'>" +
            "       vname like concat('%',#{vname},'%')" +
            "   </if>" +
            " </where>" +
            "   <if test='sort!=null'>" +
            "       order by ${sort}" +
            "   </if>" +
            "</script>")
    // <resultMap> => @Results
    @Results(
            id = "rm1",
            value = {
                    @Result(column = "id", property = "id", id = true),
                    @Result(column = "id", property = "items",
                    many = @Many(
                            select = "com.example.vote.mapper.TpItemsMapper.selectByVid"
                    )),
                    @Result(column = "id", property = "userCnt",
                    one = @One( select = "countUserCnt" ))
            }
    )
    public List<TpVote> selectByVname(
            @Param("vname") String vname,
            @Param("sort") String sort);

    @Select("select count( DISTINCT usid) from tp_record where vid=#{vid}")
    public int countUserCnt(int vid);

    @Select("select * from tp_vote where id=#{id}")
    @ResultMap("rm1")
    TpVote selectById(int id);

}
